package com.mao.action.searchcenter;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.product.jsonVoProduct;
import com.mao.action.jsonvo.searchcenter.JsonVoSearchProductStock;
import com.mao.action.jsonvo.searchcenter.JsonVoSearchProfit;
import com.mao.action.jsonvo.searchcenter.JsonVoSearchPurchaseByProduct;
import com.mao.action.jsonvo.searchcenter.JsonVoSearchVoucherByProduct;
import com.mao.action.jsonvo.searchcenter.JsonVoVoucherProduct;
import com.mao.action.jsonvo.voucher.JsonVoVoucher;
import com.mao.action.jsonvo.voucher.JsonVoVoucherType;
import com.mao.model.organization.Organization;
import com.mao.model.organization.OrganizationClass;
import com.mao.model.product.Product;
import com.mao.model.product.ProductClass;
import com.mao.model.voucher.VoucherBase;
import com.mao.model.voucher.VoucherType;
import com.mao.model.voucher.VoucherVerityType;
import com.mao.model.warehouse.WareHouse;
import com.mao.model.warehouse.WareHouseClass;
import com.mao.service.impl.productstock.ProductStockCondition;
import com.mao.service.impl.searchvoucher.SearchVoucherCondition;
import com.mao.service.organization.IOrganizationClassService;
import com.mao.service.organization.IOrganizationService;
import com.mao.service.product.IProductClassService;
import com.mao.service.product.IProductService;
import com.mao.service.productstock.IProductStockService;
import com.mao.service.searchvoucher.ISearchVoucherService;
import com.mao.service.warehouse.IWareHouseClassService;
import com.mao.service.warehouse.IWareHouseService;
import com.mao.util.SessionHelper;
import com.mao.util.Validator;

public class SearchVoucherAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private List<JsonVoVoucherType> voucherTypes;

	private ISearchVoucherService searchVoucherService;
	private IProductClassService<ProductClass> productClassService;
	private IOrganizationClassService<OrganizationClass> organizationClassService;
	private IWareHouseClassService<WareHouseClass> wareHouseClassService;

	private IProductService<Product> productService;
	private IOrganizationService<Organization> orgService;
	private IWareHouseService<WareHouse> wareHouseService;

	private IProductStockService productStockService;

	private SearchVoucherCondition searchVoucherCondition;
	private ProductStockCondition psdCondtion; // 商品库存的Condition

	private Map<String, Object> result;

	private Integer page = 1;
	private Integer rows = 0;

	private List<VoucherBase> voucherList;
	private List<JsonVoVoucher> voVoucherList;
	private List<jsonVoProduct> voProsList;
	private List<JsonVoSearchProductStock> voStocksList;

	private VoucherBase vb;

	private Long voucherId;

	private ProductClass pc;
	private OrganizationClass oc;
	private WareHouseClass wc;

	private Long pcId; // 商品分类ID
	private Long ocId;
	private Long wcId;

	private long pid; // 商品ID
	private long oid; // 供应商ID
	private long wid; // 仓库ID

	private boolean flag = true;

	private String stime;
	private String etime;
	private String voucherTypeStr;
	private String searchProName;
	private String searchOrgName;
	private String searchWhName;
	
	private String queryVoucherType;

	// 跳转到单据查询页面
	public String execute() {
		return SUCCESS;
	}

	// 单据查询
	public String searchVoucher() {
		if (searchVoucherCondition != null) {
			Long storeid = SessionHelper.getSessionUser().getStore().getId();
			result = new HashMap<String, Object>();
			voucherList = searchVoucherService.searchVoucher(storeid, page,
					rows, searchVoucherCondition);
			JsonVoVoucher voVoucher = null;
			VoucherBase vb = null;
			voVoucherList = new ArrayList<JsonVoVoucher>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			// Map<String, Object> attsMap = null;
			for (int i = 0; i < voucherList.size(); i++) {
				voVoucher = new JsonVoVoucher();
				// attsMap = new HashMap<String, Object>();
				vb = voucherList.get(i);
				voVoucher.setId(vb.getId());
				voVoucher.setVoucherNo(vb.getVoucherNo());
				voVoucher.setNo(vb.getNo());
				voVoucher.setWareHouse(vb.getWareHouse().getName());
				voVoucher.setOrg(vb.getOrg().getName());
				voVoucher.setTransactionAmount(vb.getTransactionAmount());
				voVoucher.setReceiveAmount(vb.getReceiveAmount().toString());
				voVoucher.setNote(vb.getNote());
				voVoucher.setDateTime(sdf.format(vb.getDatetime()));
				voVoucher.setIsverity(vb.getIsverity());
				voVoucher.setType(vb.getVoucherType()); // 单据类型 页面打开详情用
				voVoucherList.add(voVoucher);
			}
			result.put("total", searchVoucherService.searchVoucherCount(
					storeid, page, rows, searchVoucherCondition));
			result.put("rows", voVoucherList);
		}
		return SUCCESS;
	}

	// 获取已审核未审核状态
	public String getVerityType() {
		VoucherVerityType[] voucherTypesArray = VoucherVerityType.values();
		voucherTypes = new ArrayList<JsonVoVoucherType>();
		JsonVoVoucherType jvt = null;
		jvt = new JsonVoVoucherType();
		jvt.setId("");
		jvt.setText("全部");
		voucherTypes.add(jvt);
		for (VoucherVerityType v : voucherTypesArray) {
			jvt = new JsonVoVoucherType();
			jvt.setId(v.toString());
			jvt.setText(v.getCaption());
			voucherTypes.add(jvt);
		}
		return SUCCESS;
	}

	// 单据查询 - 每张单据中的商品
	public String getProductsByVoucher() {
		if (voucherId != null) {
			List<JsonVoVoucherProduct> pList = searchVoucherService
					.searchProductsByVoucher(voucherId,
							searchVoucherCondition.getVoucherType());
			result = new HashMap<String, Object>();
			result.put("rows", pList);
			result.put("total", Integer.MAX_VALUE);
		}
		return SUCCESS;
	}

	// 获取单据类型
	public String getVoucherTypesJson() {
		VoucherType[] voucherTypesArray = VoucherType.values();
		voucherTypes = new ArrayList<JsonVoVoucherType>();
		JsonVoVoucherType jvt = null;
		for (VoucherType v : voucherTypesArray) {
			jvt = new JsonVoVoucherType();
			jvt.setId(v.toString());
			jvt.setText(v.getCaption());
			voucherTypes.add(jvt);
		}
		return SUCCESS;
	}
	
//导出开始-----------------------------------------------------------------------------

	/**
	 * 按商品导出商品
	 * @return
	 */
	public String exportExcel() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setStime(stime);
		searchVoucherCondition.setEtime(etime);
		searchVoucherCondition.setSearchProName(searchProName);
		if(searchProName != null && !"".equals(searchProName)){
			pcId = null;
		}
		if (pcId != null && pcId != 0) {
			searchVoucherCondition.setPcId(pcId);
			pc = productClassService.getProductClassById(pcId);
			searchVoucherCondition.setTreePath(pc.getTreePath());
		}
	

		if (voucherTypeStr.equals("warehouse")) {
			searchVoucherCondition.setVoucherType(VoucherType.warehouse);
			searchVoucherCondition.setTitle("进货统计-按商品");
		//} else if (voucherTypeStr.equals("returnwarehouse")) {
		//	searchVoucherCondition.setVoucherType(VoucherType.returnwarehouse);
		//	searchVoucherCondition.setTitle("进货退货统计-按商品");
		} else if (voucherTypeStr.equals("delivery")) {
			searchVoucherCondition.setVoucherType(VoucherType.delivery);
			searchVoucherCondition.setTitle("销售统计-按商品");
		//} else if (voucherTypeStr.equals("returndelivery")) {
		//	searchVoucherCondition.setVoucherType(VoucherType.returndelivery);
		//	searchVoucherCondition.setTitle("销售退货统计-按商品");
		}
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());

		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition",
					"attachment; filename="
							+ new String(searchVoucherCondition.getTitle()
									.getBytes("GB2312"), "iso8859_1") + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			searchVoucherService.exportExcel(searchVoucherCondition, 1,
					Integer.MAX_VALUE, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}

	/**
	 * 按供应商导出商品
	 * @return
	 */
	public String exportExcelOrgs() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setStime(stime);
		searchVoucherCondition.setEtime(etime);
		searchVoucherCondition.setSearchOrgName(searchOrgName);
		if(searchOrgName != null && !"".equals(searchOrgName)){
			ocId = null;
		}
		if(ocId != null && ocId != 0){
			searchVoucherCondition.setOcId(ocId);
			oc = organizationClassService.getOrganizationClassById(ocId);
			searchVoucherCondition.setTreePath(oc.getTreePath());
		}

		if (voucherTypeStr.equals("warehouse")) {
			searchVoucherCondition.setVoucherType(VoucherType.warehouse);
			searchVoucherCondition.setTitle("进货统计-按供应商");
		//} else if (voucherTypeStr.equals("returnwarehouse")) {
		//	searchVoucherCondition.setVoucherType(VoucherType.returnwarehouse);
		//	searchVoucherCondition.setTitle("进货退货统计-按供应商");
		} else if (voucherTypeStr.equals("delivery")) {
			searchVoucherCondition.setVoucherType(VoucherType.delivery);
			searchVoucherCondition.setTitle("销售统计-按供应商");
		//} else if (voucherTypeStr.equals("returndelivery")) {
		//	searchVoucherCondition.setVoucherType(VoucherType.returndelivery);
		//	searchVoucherCondition.setTitle("销售退货统计-按供应商");
		}

		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser().getStore().getId());

		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition",
					"attachment; filename="
							+ new String(searchVoucherCondition.getTitle()
									.getBytes("GB2312"), "iso8859_1") + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			searchVoucherService.exportExcelOrgs(searchVoucherCondition, 1,
					Integer.MAX_VALUE, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}

	/**
	 * 按仓库导出商品
	 * @return
	 */
	public String exportExcelWHs() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setStime(stime);
		searchVoucherCondition.setEtime(etime);
		searchVoucherCondition.setSearchWhName(searchWhName);
		if(searchWhName != null && !"".equals(searchWhName)){
			wcId = null;
		}
		if (wcId != null && wcId != 0) {
			searchVoucherCondition.setPcId(wcId);
			wc = wareHouseClassService.getWareHouseClassById(wcId);
			searchVoucherCondition.setTreePath(wc.getTreePath());
		}

		if (voucherTypeStr.equals("warehouse")) {
			searchVoucherCondition.setVoucherType(VoucherType.warehouse);
			searchVoucherCondition.setTitle("进货统计-按仓库");
//		} else if (voucherTypeStr.equals("returnwarehouse")) {
//			searchVoucherCondition.setVoucherType(VoucherType.returnwarehouse);
//			searchVoucherCondition.setTitle("进货退货统计-按仓库");
		} else if (voucherTypeStr.equals("delivery")) {
			searchVoucherCondition.setVoucherType(VoucherType.delivery);
			searchVoucherCondition.setTitle("销售统计-按仓库");
//		} else if (voucherTypeStr.equals("returndelivery")) {
//			searchVoucherCondition.setVoucherType(VoucherType.returndelivery);
//			searchVoucherCondition.setTitle("销售退货统计-按仓库");
		}

		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());

		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition",
					"attachment; filename="
							+ new String(searchVoucherCondition.getTitle()
									.getBytes("GB2312"), "iso8859_1") + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			searchVoucherService.exportExcelWHs(searchVoucherCondition, 1,
					Integer.MAX_VALUE, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}

	// 库存余量
	public String exportRemainStock() {
		if (psdCondtion == null) {
			psdCondtion = new ProductStockCondition();
		}
		psdCondtion.setStime(stime);
		psdCondtion.setEtime(etime);
		psdCondtion.setSearchProName(searchProName);
		if(searchProName != null && !"".equals(searchProName)){
			pcId = null;
		}
		String title = "（所有商品类别）库存余量";
		if (pcId != null && pcId != 0) {
			psdCondtion.setPcId(pcId);
			pc = productClassService.getProductClassById(pcId);
			psdCondtion.setTreePath(pc.getTreePath());
			title = pc.getName() + "库存余量";
		}
		psdCondtion.setName(title);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ new String(title.getBytes("GB2312"), "iso8859_1")
					+ ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型
			
			productStockService.exportRemainStock(psdCondtion, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}

	// 导出商品盈利情况
	public String exportProfit() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setStime(stime);
		searchVoucherCondition.setEtime(etime);
		searchVoucherCondition.setSearchProName(searchProName);
		if(searchProName != null && !"".equals(searchProName)){
			pcId = null;
		}
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());

		String title = "（所有商品类别）盈利情况";
		if (pcId != null && pcId != 0) {
			searchVoucherCondition.setPcId(pcId);
			pc = productClassService.getProductClassById(pcId);
			searchVoucherCondition.setTreePath(pc.getTreePath());
			title = pc.getName() + "盈利情况";
		}
		
		searchVoucherCondition.setTitle(title);
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ new String(title.getBytes("GB2312"), "iso8859_1")
					+ ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			productStockService.exportProfit(searchVoucherCondition, 1,
					Integer.MAX_VALUE, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}

	/*
	 * 采购查询
	 */
	// 按商品
	public String toSearchPurchaseByProduct() {
		return SUCCESS;
	}

	// 查询统计-按商品
	public String searchPurchaseByProduct() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		if (!Validator.isEmpty(searchVoucherCondition.getPcId())) {
			searchVoucherCondition.setPcId(searchVoucherCondition.getPcId());
			pc = productClassService.getProductClassById(searchVoucherCondition.getPcId());
			searchVoucherCondition.setTreePath(pc.getTreePath());
		}
		searchVoucherCondition.setVoucherType(VoucherType.warehouse);
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser().getStore().getId());
		
		List<JsonVoSearchPurchaseByProduct> list = searchVoucherService
				.getSearchByProduct(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.getSearchByProductCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 进货统计-按商品 明细查询
	public String searchVoucherByProId() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.warehouse);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("warehouse,returnwarehouse");
		}
		List<JsonVoSearchVoucherByProduct> list = searchVoucherService.searchVoucherByProduct(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService.searchVoucherByProductCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 进货统计-按商品-详细 导出excel
	public String exportWareHouseDetail() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.warehouse);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("warehouse,returnwarehouse");
		}

		String title = "";
		if (!Validator.isEmpty(pid) && pid != 0) {
			searchVoucherCondition.setProId(pid);
			Product product = productService.getProductById(pid);
			title = "商品【" + product.getName() + "】的单据详情";
		} else if (!Validator.isEmpty(oid) && oid != 0) {
			searchVoucherCondition.setOrgId(oid);
			Organization org = orgService.getOrganizationById(oid);
			title = "供应商【" + org.getName() + "】的单据详情";
		} else if (!Validator.isEmpty(wid) && wid != 0) {
			searchVoucherCondition.setWhId(wid);
			WareHouse wareHouse = wareHouseService.getWareHouseById(wid);
			title = "仓库【" + wareHouse.getName() + "】的单据详情";
		}

		searchVoucherCondition.setTitle(title);
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ new String(title.getBytes("GB2312"), "iso8859_1")
					+ ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			searchVoucherService.exportWareHouseDetail(searchVoucherCondition,
					1, Integer.MAX_VALUE, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}


	// 销售统计-按商品-详细 导出excel
	public String exportDeliveryDetail() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.delivery);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("delivery,returndelivery");
		}
		
		String title = "";
		if (!Validator.isEmpty(pid) && pid != 0) {
			searchVoucherCondition.setProId(pid);
			Product product = productService.getProductById(pid);
			title = "商品【" + product.getName() + "】的单据详情";
		} else if (!Validator.isEmpty(oid) && oid != 0) {
			searchVoucherCondition.setOrgId(oid);
			Organization org = orgService.getOrganizationById(oid);
			title = "供应商【" + org.getName() + "】的单据详情";
		} else if (!Validator.isEmpty(wid) && wid != 0) {
			searchVoucherCondition.setWhId(wid);
			WareHouse wareHouse = wareHouseService.getWareHouseById(wid);
			title = "仓库【" + wareHouse.getName() + "】的单据详情";
		}

		searchVoucherCondition.setTitle(title);
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ new String(title.getBytes("GB2312"), "iso8859_1")
					+ ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			searchVoucherService.exportDeliveryDetail(searchVoucherCondition,
					1, Integer.MAX_VALUE, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}

	// 按供应商
	public String toSearchPurchaseByOrg() {
		return SUCCESS;
	}

	// 进货统计-按往来单位
	public String searchPurchaseByOrg() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		
		if (ocId != null && ocId != 0) {
			searchVoucherCondition.setPcId(ocId);
			oc = organizationClassService.getOrganizationClassById(ocId);
			searchVoucherCondition.setTreePath(oc.getTreePath());
		}
		
		searchVoucherCondition.setVoucherType(VoucherType.warehouse);
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());
		List<JsonVoSearchPurchaseByProduct> list = searchVoucherService
				.getSearchPurchaseByOrg(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.getSearchPurchaseByOrgCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 查询统计-按往来单位 明细查询
	public String searchVoucherByOrgId() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.warehouse);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("warehouse,returnwarehouse");
		}
		List<JsonVoSearchVoucherByProduct> list = searchVoucherService
				.searchVoucherByOrg(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.searchVoucherByOrgCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 按仓库
	public String toSearchPurchaseByWH() {
		return SUCCESS;
	}

	// 查询统计-按仓库
	public String searchPurchaseByWH() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}

		if (wcId != null && wcId != 0) {
			searchVoucherCondition.setPcId(wcId);
			wc = wareHouseClassService.getWareHouseClassById(wcId);
			searchVoucherCondition.setTreePath(wc.getTreePath());
		}
		searchVoucherCondition.setVoucherType(VoucherType.warehouse);
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser().getStore().getId());
		List<JsonVoSearchPurchaseByProduct> list = searchVoucherService
				.getSearchPurchaseByWH(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.getSearchPurchaseByWHCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 查询统计-按仓库 明细查询
	public String searchVoucherByWHId() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.warehouse);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("warehouse,returnwarehouse");
		}
		List<JsonVoSearchVoucherByProduct> list = searchVoucherService.searchVoucherByWH(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.searchVoucherByWHCount(searchVoucherCondition));
		return SUCCESS;
	}

	/*
	 * 销售统计
	 */
	// 按商品
	public String toSearchSaleByProduct() {
		return SUCCESS;
	}

	// 销售统计-按商品
	public String searchSaleByProduct() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}

		if (pcId != null && pcId != 0) {
			searchVoucherCondition.setPcId(pcId);
			pc = productClassService.getProductClassById(pcId);
			searchVoucherCondition.setTreePath(pc.getTreePath());
		}
		searchVoucherCondition.setVoucherType(VoucherType.delivery);
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());
		List<JsonVoSearchPurchaseByProduct> list = searchVoucherService
				.getSearchByProduct(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.getSearchByProductCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 销售统计-按商品 详情
	public String searchSVoucherByProId() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.delivery);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("delivery,returndelivery");
		}
		List<JsonVoSearchVoucherByProduct> list = searchVoucherService
				.searchVoucherByProduct(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.searchVoucherByProductCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 销售统计-按往来单位
	public String toSearchSaleByOrg() {
		return SUCCESS;
	}

	// 销售统计-按往来单位
	public String searchSaleByOrg() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}

		if (ocId != null && ocId != 0) {
			searchVoucherCondition.setOcId(ocId);
			oc = organizationClassService.getOrganizationClassById(ocId);
			searchVoucherCondition.setTreePath(oc.getTreePath());
		}
		searchVoucherCondition.setVoucherType(VoucherType.delivery);
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());
		List<JsonVoSearchPurchaseByProduct> list = searchVoucherService
				.getSearchPurchaseByOrg(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.getSearchPurchaseByOrgCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 销售统计-按供应商 详情
	public String searchSVoucherByOrgId() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.delivery);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("delivery,returndelivery");
		}
		List<JsonVoSearchVoucherByProduct> list = searchVoucherService
				.searchVoucherByOrg(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.searchVoucherByOrgCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 销售统计-按仓库
	public String toSearchSaleByWH() {
		return SUCCESS;
	}

	// 销售统计-按仓库
	public String searchSaleByWH() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}

		if (wcId != null && wcId != 0) {
			searchVoucherCondition.setPcId(wcId);
			wc = wareHouseClassService.getWareHouseClassById(wcId);
			searchVoucherCondition.setTreePath(wc.getTreePath());
		}
		searchVoucherCondition.setVoucherType(VoucherType.delivery);
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());
		List<JsonVoSearchPurchaseByProduct> list = searchVoucherService
				.getSearchPurchaseByWH(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.getSearchPurchaseByWHCount(searchVoucherCondition));
		return SUCCESS;
	}

	// 销售统计-按仓库 明细查询
	public String searchSaleByWHId() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setVoucherType(VoucherType.delivery);
		if(!Validator.isEmpty(queryVoucherType)){
			searchVoucherCondition.setQueryVoucherType(queryVoucherType);
		}else{
			searchVoucherCondition.setQueryVoucherType("delivery,returndelivery");
		}
		List<JsonVoSearchVoucherByProduct> list = searchVoucherService
				.searchVoucherByWH(searchVoucherCondition, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService
				.searchVoucherByWHCount(searchVoucherCondition));
		return SUCCESS;
	}
	
	/*
	 * 库存查询
	 */
	// 跳到库存查询
	public String toSearchStock() {
		return SUCCESS;
	}

	public String searchStockByPcId() {
		ProductClass pc = null;
		if (psdCondtion == null) {
			psdCondtion = new ProductStockCondition();
		}
		if (pcId != null) {
			pc = productClassService.getProductClassById(pcId);
			psdCondtion.setTreePath(pc.getTreePath());
		}
		List<JsonVoSearchProductStock> list = productStockService.searchStockByPcId(psdCondtion, page, rows);
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total",productStockService.searchStockByPcIdCount(psdCondtion));
		return SUCCESS;
	}

	/**
	 * 查询库存的子库存情况
	 * @return
	 */
	public String searchStockByPro() {
		Product product = productService.getProductById(pid);
		if(searchVoucherCondition == null){
			searchVoucherCondition = new SearchVoucherCondition();
		}
		searchVoucherCondition.setPro(product);
		voStocksList = productStockService.findStocksByPro(searchVoucherCondition);
		return SUCCESS;
	}

	/*
	 * 盈利查询
	 * 
	 * @return
	 */
	public String toSearchProfit() {
		return SUCCESS;
	}

	public String searchProfit() {
		if (searchVoucherCondition == null) {
			searchVoucherCondition = new SearchVoucherCondition();
		}
		
		if (pcId != null && pcId != 0) {
			searchVoucherCondition.setPcId(pcId);
			pc = productClassService.getProductClassById(pcId);
			searchVoucherCondition.setTreePath(pc.getTreePath());
		}
		
		searchVoucherCondition.setStoreid(SessionHelper.getSessionUser()
				.getStore().getId());
		List<JsonVoSearchProfit> list = searchVoucherService.searchProfit(
				searchVoucherCondition, page, rows);
		
		result = new HashMap<String, Object>();
		result.put("rows", list);
		result.put("total", searchVoucherService.searchProfitCount(searchVoucherCondition));
		return SUCCESS;
	}

	
	
	public List<JsonVoVoucherType> getVoucherTypes() {
		return voucherTypes;
	}

	public void setVoucherTypes(List<JsonVoVoucherType> voucherTypes) {
		this.voucherTypes = voucherTypes;
	}

	public ISearchVoucherService getSearchVoucherService() {
		return searchVoucherService;
	}

	public void setSearchVoucherService(
			ISearchVoucherService searchVoucherService) {
		this.searchVoucherService = searchVoucherService;
	}

	public SearchVoucherCondition getSearchVoucherCondition() {
		return searchVoucherCondition;
	}

	public void setSearchVoucherCondition(
			SearchVoucherCondition searchVoucherCondition) {
		this.searchVoucherCondition = searchVoucherCondition;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public List<VoucherBase> getVoucherList() {
		return voucherList;
	}

	public void setVoucherList(List<VoucherBase> voucherList) {
		this.voucherList = voucherList;
	}

	public List<JsonVoVoucher> getVoVoucherList() {
		return voVoucherList;
	}

	public void setVoVoucherList(List<JsonVoVoucher> voVoucherList) {
		this.voVoucherList = voVoucherList;
	}

	public VoucherBase getVb() {
		return vb;
	}

	public void setVb(VoucherBase vb) {
		this.vb = vb;
	}

	public Long getVoucherId() {
		return voucherId;
	}

	public void setVoucherId(Long voucherId) {
		this.voucherId = voucherId;
	}

	public ProductClass getPc() {
		return pc;
	}

	public void setPc(ProductClass pc) {
		this.pc = pc;
	}

	public IProductClassService<ProductClass> getProductClassService() {
		return productClassService;
	}

	public void setProductClassService(
			IProductClassService<ProductClass> productClassService) {
		this.productClassService = productClassService;
	}

	public IProductService<Product> getProductService() {
		return productService;
	}

	public void setProductService(IProductService<Product> productService) {
		this.productService = productService;
	}

	public List<jsonVoProduct> getVoProsList() {
		return voProsList;
	}

	public void setVoProsList(List<jsonVoProduct> voProsList) {
		this.voProsList = voProsList;
	}

	public Long getPcId() {
		return pcId;
	}

	public void setPcId(Long pcId) {
		this.pcId = pcId;
	}

	public long getPid() {
		return pid;
	}

	public void setPid(long pid) {
		this.pid = pid;
	}

	public long getOid() {
		return oid;
	}

	public void setOid(long oid) {
		this.oid = oid;
	}

	public long getWid() {
		return wid;
	}

	public void setWid(long wid) {
		this.wid = wid;
	}

	public IProductStockService getProductStockService() {
		return productStockService;
	}

	public void setProductStockService(IProductStockService productStockService) {
		this.productStockService = productStockService;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getStime() {
		return stime;
	}

	public void setStime(String stime) {
		this.stime = stime;
	}

	public String getEtime() {
		return etime;
	}

	public void setEtime(String etime) {
		this.etime = etime;
	}

	public String getVoucherTypeStr() {
		return voucherTypeStr;
	}

	public void setVoucherTypeStr(String voucherTypeStr) {
		this.voucherTypeStr = voucherTypeStr;
	}

	public IOrganizationService<Organization> getOrgService() {
		return orgService;
	}

	@Autowired
	public void setOrgService(IOrganizationService<Organization> orgService) {
		this.orgService = orgService;
	}

	public IWareHouseService<WareHouse> getWareHouseService() {
		return wareHouseService;
	}

	@Autowired
	public void setWareHouseService(
			IWareHouseService<WareHouse> wareHouseService) {
		this.wareHouseService = wareHouseService;
	}

	public Long getOcId() {
		return ocId;
	}

	public void setOcId(Long ocId) {
		this.ocId = ocId;
	}

	public Long getWcId() {
		return wcId;
	}

	public void setWcId(Long wcId) {
		this.wcId = wcId;
	}

	public ProductStockCondition getPsdCondtion() {
		return psdCondtion;
	}

	public void setPsdCondtion(ProductStockCondition psdCondtion) {
		this.psdCondtion = psdCondtion;
	}

	public List<JsonVoSearchProductStock> getVoStocksList() {
		return voStocksList;
	}

	public void setVoStocksList(List<JsonVoSearchProductStock> voStocksList) {
		this.voStocksList = voStocksList;
	}

	public IOrganizationClassService<OrganizationClass> getOrganizationClassService() {
		return organizationClassService;
	}

	public void setOrganizationClassService(
			IOrganizationClassService<OrganizationClass> organizationClassService) {
		this.organizationClassService = organizationClassService;
	}

	public IWareHouseClassService<WareHouseClass> getWareHouseClassService() {
		return wareHouseClassService;
	}

	public void setWareHouseClassService(
			IWareHouseClassService<WareHouseClass> wareHouseClassService) {
		this.wareHouseClassService = wareHouseClassService;
	}

	public OrganizationClass getOc() {
		return oc;
	}

	public void setOc(OrganizationClass oc) {
		this.oc = oc;
	}

	public WareHouseClass getWc() {
		return wc;
	}

	public void setWc(WareHouseClass wc) {
		this.wc = wc;
	}

	public String getQueryVoucherType() {
		return queryVoucherType;
	}

	public void setQueryVoucherType(String queryVoucherType) {
		this.queryVoucherType = queryVoucherType;
	}

	public String getSearchProName() {
		return searchProName;
	}

	public void setSearchProName(String searchProName) {
		this.searchProName = searchProName;
	}

	public String getSearchOrgName() {
		return searchOrgName;
	}

	public void setSearchOrgName(String searchOrgName) {
		this.searchOrgName = searchOrgName;
	}

	public String getSearchWhName() {
		return searchWhName;
	}

	public void setSearchWhName(String searchWhName) {
		this.searchWhName = searchWhName;
	}
	
}
